package com.baidu.mpks.permission.service;

import com.baidu.mpks.permission.dao.PermissionDataRoleDao;
import com.baidu.mpks.permission.dao.PermissionDataUserDao;
import com.baidu.mpks.permission.domain.PermissionData;
import com.baidu.mpks.permission.domain.PermissionDataRole;
import com.baidu.mpks.permission.dto.AtomDataRolePermissionDto;
import com.baidu.mpks.permission.dto.AuthInfoDto;
import com.baidu.mpks.permission.dto.DataRolePermissionDto;
import com.baidu.mpks.permission.dto.UserDataPermissionDto;
import com.baidu.mpks.schema.dao.UserSchemaClassDao;
import com.baidu.mpks.schema.dao.UserSchemaPropertyDao;
import com.baidu.mpks.schema.domain.UserSchemaAll;
import com.baidu.mpks.schema.domain.UserSchemaClass;
import com.baidu.mpks.schema.domain.UserSchemaProperty;
import com.baidu.mpks.schema.service.UserSchemaManageService;
import com.baidu.mpks.schema.service.UserSchemaPermissionService;
import com.baidu.mpks.schema.service.UserSchemaService;
import com.baidu.mpks.tree.dao.UserTreeNodeDao;
import com.baidu.mpks.tree.service.UserTreeService;
import com.baidu.mpks.util.JacksonUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.util.set.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PermissionManageServiceImpl implements PermissionManageService{

    @Autowired
    PermissionDataRoleDao permissionDataRoleDao;
    @Autowired
    PermissionDataUserDao permissionDataUserDao;
    @Autowired
    UserSchemaService userSchemaService;
    @Autowired
    UserSchemaManageService userSchemaManageService;
    @Autowired
    UserSchemaClassDao userSchemaClassDao;
    @Autowired
    UserSchemaPropertyDao userSchemaPropertyDao;
    @Autowired
    UserTreeNodeDao userTreeNodeDao;
    @Autowired
    PermissionCacheService permissionCacheService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    UserTreeService userTreeService;
    @Autowired
    UserSchemaPermissionService userSchemaPermissionService;
    @Value("${permission.custom.nodeid.enabled}")
    private Boolean customNodeIdEnabled;
    @Value("${permission.custom.nodeid.list}")
    private String customNodeIdStr;
    private static final String CUSTOM_NODEID_SEPARATOR = ",";

    public PermissionManageServiceImpl() {
    }

    public Map<String, Set<String>> getOpenData(Set<PermissionDataRole> openData) {
        Map<String, Set<String>> openDataMap = (Map)openData.stream().collect(Collectors.groupingBy(PermissionDataRole::getOp, Collectors.mapping(PermissionDataRole::getDataId, Collectors.toSet())));
        return openDataMap;
    }

    public Map<String, Set<String>> getOpenDataV3(Set<PermissionDataRole> openData) {
        Map<String, Set<String>> openDataMap = (Map)openData.stream().collect(Collectors.groupingBy(PermissionDataRole::getDataId, Collectors.mapping(PermissionDataRole::getOp, Collectors.toSet())));
        return openDataMap;
    }

    public Map<String, Set<String>> setSourcePermission(Map<String, List<PermissionData>> rolePermissionDataMap, Map<String, Set<String>> sourceAuthInfo, Set<PermissionDataRole> openData) {
        List<PermissionData> sourceAuthOriList = (List)rolePermissionDataMap.get(PermissionService.IDTYPE.SOURCE.getName());
        Map<String, Set<String>> allSourceAuthInfo = new HashMap();
        int var8;
        HashSet allSourceTempList = new HashSet();
        if (sourceAuthOriList != null) {
            sourceAuthInfo = (Map)sourceAuthOriList.stream().collect(Collectors.groupingBy(PermissionData::getOp, Collectors.mapping(PermissionData::getDataId, Collectors.toSet())));
            PermissionService.OP[] var6 = PermissionService.OP.values();
            int var7 = var6.length;

            for(var8 = 0; var8 < var7; ++var8) {
                PermissionService.OP op = var6[var8];
                Set<Integer> nodesIncludeChildren = new HashSet();
                Set<String> sourceAuthSetByOP = (Set)sourceAuthInfo.get(op.getName());
                allSourceTempList = new HashSet();
                if (sourceAuthSetByOP != null) {
                    sourceAuthSetByOP.forEach((item) -> {
                        nodesIncludeChildren.addAll(this.userTreeService.getChildrensIncludeSelf(Integer.parseInt(item)));
                        allSourceTempList.addAll((Collection)nodesIncludeChildren.stream().map((data) -> {
                            return data.toString();
                        }).collect(Collectors.toSet()));
                    });
                }

                sourceAuthInfo.put(op.getName(), allSourceTempList);
            }
        }

        Map<String, Set<String>> openNodeMap = new HashMap();
        String opType;
        if (openData != null) {
            openNodeMap = this.getOpenData(openData);
            Iterator var14 = ((Map)openNodeMap).entrySet().iterator();

            while(var14.hasNext()) {
                Map.Entry<String, Set<String>> entry = (Map.Entry)var14.next();
                Set<String> nodeOpenDataSet = new HashSet();
                nodeOpenDataSet.addAll((Collection)entry.getValue());
                Iterator var19 = ((Set)entry.getValue()).iterator();

                while(var19.hasNext()) {
                    opType = (String)var19.next();
                    Set<Integer> nodesIncludeChildren = this.userTreeService.getChildrensNotIncludeSelf(Integer.parseInt(opType));
                    if (CollectionUtils.isNotEmpty(nodesIncludeChildren)) {
                        nodeOpenDataSet.addAll((Collection)nodesIncludeChildren.stream().map(Object::toString).collect(Collectors.toSet()));
                    }
                }

                ((Map)openNodeMap).put(entry.getKey(), nodeOpenDataSet);
            }
        }

        PermissionService.OP[] var15 = PermissionService.OP.values();
        var8 = var15.length;

        for(int var18 = 0; var18 < var8; ++var18) {
            PermissionService.OP op = var15[var18];
            opType = op.getName();
            allSourceTempList = new HashSet();
            if (((Map)openNodeMap).get(opType) != null) {
                allSourceTempList.addAll((Collection)((Map)openNodeMap).get(opType));
            }

            if (sourceAuthInfo.get(opType) != null) {
                allSourceTempList.addAll((Collection)sourceAuthInfo.get(opType));
            }

            allSourceAuthInfo.put(opType, allSourceTempList);
        }

        return allSourceAuthInfo;
    }

    public Map<String, Set<String>> setSourcePermissionV3(Map<String, List<PermissionData>> rolePermissionDataMap, Set<PermissionDataRole> openData) {
        new HashMap();
        List<PermissionData> sourceAuthOriList = (List)rolePermissionDataMap.get(PermissionService.IDTYPE.SOURCE.getName());
        Map<String, Set<String>> allSourceAuthInfo = new HashMap();
        if (sourceAuthOriList != null) {
            Map<String, Set<String>> sourceAuthInfo = (Map)sourceAuthOriList.stream().collect(Collectors.groupingBy(PermissionData::getDataId, Collectors.mapping(PermissionData::getOp, Collectors.toSet())));
            Iterator var6 = sourceAuthInfo.keySet().iterator();

            label55:
            while(true) {
                String key;
                Set nodesIncludeChildren;
                do {
                    if (!var6.hasNext()) {
                        break label55;
                    }

                    key = (String)var6.next();
                    if (!allSourceAuthInfo.containsKey(key)) {
                        allSourceAuthInfo.put(key, sourceAuthInfo.get(key));
                    } else {
                        ((Set)allSourceAuthInfo.get(key)).addAll((Collection)sourceAuthInfo.get(key));
                    }

                    nodesIncludeChildren = this.userTreeService.getChildrensNotIncludeSelf(Integer.parseInt(key));
                } while(!CollectionUtils.isNotEmpty(nodesIncludeChildren));

                Integer nodeId;
                HashSet allPermissions;
                for(Iterator var9 = nodesIncludeChildren.iterator(); var9.hasNext(); allSourceAuthInfo.put(nodeId.toString(), allPermissions)) {
                    nodeId = (Integer)var9.next();
                    allPermissions = Sets.newHashSet((Iterable)allSourceAuthInfo.get(key));
                    if (CollectionUtils.isNotEmpty((Collection)sourceAuthInfo.get(nodeId.toString()))) {
                        allPermissions.addAll((Collection)sourceAuthInfo.get(nodeId.toString()));
                    }
                }
            }
        }

        Map<String, Set<String>> openNodeMap = new HashMap();
        if (openData != null) {
            openNodeMap = this.getOpenDataV3(openData);
            Map<String, Set<String>> nodeOpenMap = new HashMap();
            Iterator var15 = ((Map)openNodeMap).entrySet().iterator();

            while(var15.hasNext()) {
                Map.Entry<String, Set<String>> entry = (Map.Entry)var15.next();
                Set<Integer> nodesIncludeChildren = this.userTreeService.getChildrensNotIncludeSelf(Integer.parseInt((String)entry.getKey()));
                Iterator var18 = nodesIncludeChildren.iterator();

                while(var18.hasNext()) {
                    Integer nodeId = (Integer)var18.next();
                    nodeOpenMap.put(nodeId.toString(), entry.getValue());
                }
            }

            ((Map)openNodeMap).putAll(nodeOpenMap);
        }

        return this.mergeMap((Map)openNodeMap, allSourceAuthInfo);
    }

    public Set<String> getSysProp() {
        Set<String> propSysSet = new HashSet();
        propSysSet.add("@id");
        propSysSet.add("@type");
        propSysSet.add("@dataSourceId");
        propSysSet.add("@sourceId");
        propSysSet.add("name");
        propSysSet.add("nodeId");
        propSysSet.add("@nodeid");
        return propSysSet;
    }

    private Map<String, Map<String, Set<String>>> setClassPermission(Map<String, List<PermissionData>> rolePermissionDataMap, Map<String, UserSchemaProperty> allPropMap, Map<String, UserSchemaClass> allClassMap, Set<PermissionDataRole> openProp, Set<PermissionDataRole> openClass) throws IOException {
        Map<String, Map<String, Set<String>>> fieldAuthInfo = new HashMap();
        Map<String, Set<String>> allClassPermissionMap = new HashMap();
        Map<String, Set<String>> selfClassPermissionMap = new HashMap();
        List<PermissionData> selfClassPermissionList = (List)rolePermissionDataMap.get(PermissionService.IDTYPE.TYPE.getName());
        if (selfClassPermissionList != null) {
            selfClassPermissionMap = (Map)selfClassPermissionList.stream().collect(Collectors.groupingBy(PermissionData::getOp, Collectors.mapping(PermissionData::getDataId, Collectors.toSet())));
        }

        Map<String, Set<String>> openClassPermissionMap = new HashMap();
        if (openClass != null) {
            openClassPermissionMap = this.getOpenData(openClass);
        }

        PermissionService.OP[] var11 = PermissionService.OP.values();
        int var12 = var11.length;

        for(int var13 = 0; var13 < var12; ++var13) {
            PermissionService.OP op = var11[var13];
            String opType = op.getName();
            Set<String> allClassTempList = new HashSet();
            if (((Map)openClassPermissionMap).get(opType) != null) {
                allClassTempList.addAll((Collection)((Map)openClassPermissionMap).get(opType));
            }

            if (((Map)selfClassPermissionMap).get(opType) != null) {
                allClassTempList.addAll((Collection)((Map)selfClassPermissionMap).get(opType));
            }

            allClassPermissionMap.put(opType, allClassTempList);
        }

        List<PermissionData> selfPropPermissionList = (List)rolePermissionDataMap.get(PermissionService.IDTYPE.FIELD.getName());
        Map<String, Set<String>> allPropPermissionMap = new HashMap();
        Map<String, Set<String>> selfPropPermissionMap = new HashMap();
        if (selfPropPermissionList != null) {
            selfPropPermissionMap = (Map)selfPropPermissionList.stream().collect(Collectors.groupingBy(PermissionData::getOp, Collectors.mapping(PermissionData::getDataId, Collectors.toSet())));
        }

        Map<String, Set<String>> openPropPermissionMap = new HashMap();
        if (openProp != null) {
            openPropPermissionMap = this.getOpenData(openProp);
        }

        PermissionService.OP[] var32 = PermissionService.OP.values();
        int var34 = var32.length;

        for(int var17 = 0; var17 < var34; ++var17) {
            PermissionService.OP op = var32[var17];
            String opType = op.getName();
            Set<String> allPropTempList = new HashSet();
            if (((Map)openPropPermissionMap).get(opType) != null) {
                allPropTempList.addAll((Collection)((Map)openPropPermissionMap).get(opType));
            }

            if (((Map)selfPropPermissionMap).get(opType) != null) {
                allPropTempList.addAll((Collection)((Map)selfPropPermissionMap).get(opType));
            }

            allPropPermissionMap.put(opType, allPropTempList);
        }

        Map<String, List<UserSchemaProperty>> fullTextPropMap = new HashMap();
        List<String> fullTextClassIdList = (List)this.userSchemaClassDao.getAllSys().stream().map(UserSchemaClass::getAtId).collect(Collectors.toList());
        List<UserSchemaProperty> allPropList = this.userSchemaPropertyDao.getAll();
        Map<String, List<UserSchemaProperty>> allPropDict = (Map)allPropList.stream().collect(Collectors.groupingBy(UserSchemaProperty::getDomain));
        fullTextClassIdList.forEach((item) -> {
            List<UserSchemaProperty> propertyList = (List)allPropDict.get("[\"" + item + "\"]");
            fullTextPropMap.put(item, propertyList);
        });
        PermissionService.OP[] var38 = PermissionService.OP.values();
        int var39 = var38.length;

        for(int var21 = 0; var21 < var39; ++var21) {
            PermissionService.OP op = var38[var21];
            String opType = op.getName();
            Map<String, Set<String>> classPermission = new HashMap();
            Set<String> allClassIdSet = (Set)allClassPermissionMap.get(opType);
            Set<String> allPropPermissionSet = (Set)allPropPermissionMap.get(opType);
            Map<String, Set<String>> allClassToPropPermissionMap = new HashMap();
            if (allPropPermissionSet != null) {
                allPropPermissionSet.forEach((item) -> {
                    String[] classProp = item.split("@");
                    String classId = classProp[0];
                    Set<String> propSet = new HashSet();
                    if (allClassToPropPermissionMap.get(classId) != null) {
                        propSet = (Set)allClassToPropPermissionMap.get(classId);
                    }

                    String propId = classProp[1];
                    ((Set)propSet).add(propId);
                    allClassToPropPermissionMap.put(classId, propSet);
                });
            } else {
                new HashSet();
            }

            if (allClassIdSet != null) {
                allClassIdSet.forEach((item) -> {
                    Set<String> propSysSet = this.getSysProp();
                    new HashSet();
                    Set<String> propertiesFilterSet = new HashSet();
                    Set propertiesSet;
                    if (fullTextClassIdList.contains(item)) {
                        propertiesSet = (Set)((List)fullTextPropMap.get(item)).stream().map((data) -> {
                            return JacksonUtils.getString(data.getLabel(), "zh-cn");
                        }).collect(Collectors.toSet());
                        ((Set)propertiesFilterSet).addAll(propertiesSet);
                    } else if (allClassToPropPermissionMap.get(item) != null) {
                        propertiesSet = (Set)allClassToPropPermissionMap.get(item);
                        if (propertiesSet != null && !fullTextClassIdList.contains(item)) {
                            propertiesFilterSet = (Set)propertiesSet.stream().filter((data) -> {
                                return allPropMap.get(data) != null;
                            }).map((data) -> {
                                return JacksonUtils.getString(((UserSchemaProperty)allPropMap.get(data)).getLabel(), "zh-cn");
                            }).collect(Collectors.toSet());
                        }
                    }

                    ((Set)propertiesFilterSet).addAll(propSysSet);
                    if (allClassMap.get(item) != null) {
                        String className = JacksonUtils.getString(((UserSchemaClass)allClassMap.get(item)).getLabel(), "zh-cn");
                        classPermission.put(className, propertiesFilterSet);
                    }

                });
            }

            fieldAuthInfo.put(opType, classPermission);
        }

        return fieldAuthInfo;
    }

    private Map<String, Map<String, Set<String>>> setClassPermissionV3(Map<String, List<PermissionData>> rolePermissionDataMap, Map<String, UserSchemaProperty> allPropMap, Map<String, UserSchemaClass> allClassMap, Set<PermissionDataRole> openProp, Set<PermissionDataRole> openClass) throws IOException {
        Map<String, Map<String, Set<String>>> fieldAuthInfo = new HashMap();
        new HashMap();
        Map<String, Set<String>> selfClassPermissionMap = new HashMap();
        List<PermissionData> selfClassPermissionList = (List)rolePermissionDataMap.get(PermissionService.IDTYPE.TYPE.getName());
        if (selfClassPermissionList != null) {
            selfClassPermissionMap = (Map)selfClassPermissionList.stream().collect(Collectors.groupingBy(PermissionData::getDataId, Collectors.mapping(PermissionData::getOp, Collectors.toSet())));
        }

        Map<String, Set<String>> openClassPermissionMap = new HashMap();
        if (openClass != null) {
            openClassPermissionMap = this.getOpenDataV3(openClass);
        }

        Map<String, Set<String>> allClassPermissionMap = this.mergeMap((Map)openClassPermissionMap, (Map)selfClassPermissionMap);
        List<PermissionData> selfPropPermissionList = (List)rolePermissionDataMap.get(PermissionService.IDTYPE.FIELD.getName());
        new HashMap();
        Map<String, Set<String>> selfPropPermissionMap = new HashMap();
        if (selfPropPermissionList != null) {
            selfPropPermissionMap = (Map)selfPropPermissionList.stream().collect(Collectors.groupingBy(PermissionData::getDataId, Collectors.mapping(PermissionData::getOp, Collectors.toSet())));
        }

        Map<String, Set<String>> openPropPermissionMap = new HashMap();
        if (openProp != null) {
            openPropPermissionMap = this.getOpenDataV3(openProp);
        }

        Map<String, Set<String>> allPropPermissionMap = this.mergeMap((Map)selfPropPermissionMap, (Map)openPropPermissionMap);
        Map<String, List<UserSchemaProperty>> fullTextPropMap = new HashMap();
        List<String> fullTextClassIdList = (List)this.userSchemaClassDao.getAllSys().stream().map(UserSchemaClass::getAtId).collect(Collectors.toList());
        List<UserSchemaProperty> allPropList = this.userSchemaPropertyDao.getAll();
        Map<String, List<UserSchemaProperty>> allPropDict = (Map)allPropList.stream().collect(Collectors.groupingBy(UserSchemaProperty::getDomain));
        fullTextClassIdList.forEach((item) -> {
            List<UserSchemaProperty> propertyList = (List)allPropDict.get("[\"" + item + "\"]");
            fullTextPropMap.put(item, propertyList);
        });
        Map<String, Map<String, Set<String>>> allClassToPropPermissionMap = new HashMap();
        Iterator var20 = allPropPermissionMap.entrySet().iterator();

        Map.Entry entry;
        while(var20.hasNext()) {
            entry = (Map.Entry)var20.next();
            String[] classProp = ((String)entry.getKey()).split("@");
            String classId = classProp[0];
            String propId = classProp[1];
            Map<String, Set<String>> map = new HashMap();
            if (allClassToPropPermissionMap.get(classId) != null) {
                map = (Map)allClassToPropPermissionMap.get(classId);
                ((Map)map).put(propId, entry.getValue());
            }

            ((Map)map).put(propId, entry.getValue());
            allClassToPropPermissionMap.put(classId, map);
        }

        var20 = allClassPermissionMap.entrySet().iterator();

        while(true) {
            do {
                if (!var20.hasNext()) {
                    return fieldAuthInfo;
                }

                entry = (Map.Entry)var20.next();
            } while(allClassMap.get(entry.getKey()) == null);

            Map<String, Set<String>> propertiesMap = new HashMap();
            new HashSet();
            Set<String> sysProp = this.getSysProp();
            if (entry.getValue() != null && ((Set)entry.getValue()).contains("r")) {
                ((Set)entry.getValue()).add("so");
            }

            Iterator var33;
            String sp;
            if (fullTextClassIdList.contains(entry.getKey())) {
                Set<String> propertiesSet = (Set)((List)fullTextPropMap.get(entry.getKey())).stream().map((data) -> {
                    return JacksonUtils.getString(data.getLabel(), "zh-cn");
                }).collect(Collectors.toSet());
                var33 = propertiesSet.iterator();

                while(var33.hasNext()) {
                    sp = (String)var33.next();
                    propertiesMap.put(sp, (Set<String>) entry.getValue());
                }
            } else if (allClassToPropPermissionMap.get(entry.getKey()) != null) {
                Map<String, Set<String>> map = (Map)allClassToPropPermissionMap.get(entry.getKey());
                Iterator var26 = map.entrySet().iterator();

                while(var26.hasNext()) {
                    Map.Entry<String, Set<String>> value = (Map.Entry)var26.next();
                    ((Set)value.getValue()).retainAll((Collection)entry.getValue());
                    if (((Set)value.getValue()).size() > 0 && allPropMap.get(value.getKey()) != null) {
                        String key = JacksonUtils.getString(((UserSchemaProperty)allPropMap.get(value.getKey())).getLabel(), "zh-cn");
                        propertiesMap.put(key, value.getValue());
                    }
                }
            }

            var33 = sysProp.iterator();

            while(var33.hasNext()) {
                sp = (String)var33.next();
                propertiesMap.put(sp, (Set<String>) entry.getValue());
            }

            String className = JacksonUtils.getString(((UserSchemaClass)allClassMap.get(entry.getKey())).getLabel(), "zh-cn");
            fieldAuthInfo.put(className, propertiesMap);
        }
    }

    private Map<String, Set<String>> mergeMap(Map<String, Set<String>> map1, Map<String, Set<String>> map2) {
        Set<String> keySet = new HashSet();
        if (MapUtils.isEmpty(map1)) {
            return map2;
        } else if (MapUtils.isEmpty(map2)) {
            return map1;
        } else {
            keySet.addAll(map1.keySet());
            keySet.addAll(map2.keySet());

            String key;
            HashSet values;
            for(Iterator var4 = keySet.iterator(); var4.hasNext(); map1.put(key, values)) {
                key = (String)var4.next();
                values = new HashSet();
                if (map1.get(key) != null) {
                    values.addAll((Collection)map1.get(key));
                }

                if (map2.get(key) != null) {
                    values.addAll((Collection)map2.get(key));
                }
            }

            return map1;
        }
    }

    public Map<String, List<PermissionData>> getRolePermissionDataMap(List<Integer> roleIds, String userId) {
        List<String> roles = (List)roleIds.stream().map(Objects::toString).collect(Collectors.toList());
        List<PermissionData> rolePermissionDataList = this.permissionDataRoleDao.getRoleDataAndUserDataByRoleIdsAndUserId(roles, userId);
        if (this.customNodeIdEnabled) {
            String[] nodeIdArr = StringUtils.split(this.customNodeIdStr, ",");
            Arrays.stream(nodeIdArr).forEach((nodeId) -> {
                Iterator var2 = PermissionService.OP.getNames().iterator();

                while(var2.hasNext()) {
                    String op = (String)var2.next();
                    PermissionData permissionData = new PermissionData();
                    permissionData.setDataId(nodeId);
                    permissionData.setIdType(PermissionService.IDTYPE.SOURCE.getName());
                    permissionData.setOp(op);
                    if (!rolePermissionDataList.contains(permissionData)) {
                        rolePermissionDataList.add(permissionData);
                    }
                }

            });
        }

        return (Map)rolePermissionDataList.stream().collect(Collectors.groupingBy(PermissionData::getIdType));
    }

    public Map<String, Map<String, Object>> getAllDataPermissionByUserIdAndRoleIdClassForNew(List<Integer> roleIds, String userId) throws IOException {
        Map<String, Map<String, Object>> result = new HashMap();
        PermissionService.OP[] var4 = PermissionService.OP.values();
        int var5 = var4.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            PermissionService.OP op = var4[var6];
            result.put(op.getName(), new HashMap());
        }

        List<UserSchemaClass> allClass = this.userSchemaClassDao.getAll();
        List<UserSchemaProperty> allProp = this.userSchemaPropertyDao.getAll();
        Map<String, UserSchemaProperty> allPropMap = new HashMap();
        Map<String, UserSchemaClass> allClassMap = new HashMap();
        Map<String, List<PermissionData>> rolePermissionDataMap = this.getRolePermissionDataMap(roleIds, userId);
        Map<String, Set<String>> recordAuthInfo = new HashMap();
        if (rolePermissionDataMap.get(PermissionService.IDTYPE.RECORD.getName()) != null) {
            recordAuthInfo = (Map)((List)rolePermissionDataMap.get("record")).stream().collect(Collectors.groupingBy(PermissionData::getOp, Collectors.mapping(PermissionData::getDataId, Collectors.toSet())));
        }

        Map classToPropMap;
        Map classAncestorMap;
        PermissionService.OP[] var15;
        int var16;
        int var17;
        PermissionService.OP op;
        String opType;
        Map opPermission;
        if (roleIds.contains(1)) {
            Set<String> allTreeNodes = (Set)this.userTreeNodeDao.getTreeNodes().stream().map((item) -> {
                return item.getId().toString();
            }).collect(Collectors.toSet());
            Map<String, Set<String>> allClassPropMap = new HashMap();
            classToPropMap = (Map)allProp.stream().collect(Collectors.groupingBy(UserSchemaProperty::getDomain));
            List<UserSchemaAll> schemaAll = this.userSchemaPermissionService.getSchemaAll();
            classAncestorMap = this.userSchemaPermissionService.getAncestorMap(schemaAll);
            var15 = PermissionService.OP.values();
            var16 = var15.length;

            for(var17 = 0; var17 < var16; ++var17) {
                op = var15[var17];
                opType = op.getName();
                opPermission = (Map)result.get(opType);
                allClass.forEach((item) -> {
                    if (item.getAtType().equals("Class")) {
                        String className = JacksonUtils.getString(item.getLabel(), "zh-cn");
                        String domain = "[\"" + item.getAtId() + "\"]";
                        List<UserSchemaProperty> propList = new ArrayList();
                        if (classToPropMap.get(domain) != null) {
                            propList.addAll((Collection)classToPropMap.get(domain));
                        }

                        Set<String> classList = (Set)classAncestorMap.get(item.getAtId());
                        if (classList != null) {
                            classList.forEach((classNameData) -> {
                                String parentDomain = "[\"" + classNameData + "\"]";
                                List<UserSchemaProperty> parentPropList = (List)classToPropMap.get(parentDomain);
                                if (parentPropList != null) {
                                    propList.addAll(parentPropList);
                                }

                            });
                        }

                        Set<String> propNameList = new HashSet();
                        if (propList != null) {
                            propList.forEach((data) -> {
                                propNameList.add(JacksonUtils.getString(data.getLabel(), "zh-cn"));
                            });
                        }

                        propNameList.addAll(this.getSysProp());
                        allClassPropMap.put(className, propNameList);
                    }

                });
                opPermission.put("sourceAuthInfo", allTreeNodes);
                opPermission.put("recordAuthInfo", ((Map)recordAuthInfo).get(opType) == null ? new HashSet() : ((Map)recordAuthInfo).get(opType));
                opPermission.put("fieldAuthInfo", allClassPropMap);
                result.put(opType, opPermission);
            }
        } else {
            allProp.forEach((item) -> {
                allPropMap.put(item.getAtId(), item);
            });
            allClass.forEach((item) -> {
                allClassMap.put(item.getAtId(), item);
            });
            List<String> propType = new ArrayList();
            propType.add(PermissionService.IDTYPE.FIELD.getName());
            propType.add(PermissionService.IDTYPE.SOURCE.getName());
            propType.add(PermissionService.IDTYPE.TYPE.getName());
            Set<PermissionDataRole> openData = this.permissionDataRoleDao.getOpenDataByIdTypes(propType);
            classToPropMap = (Map)openData.stream().collect(Collectors.groupingBy(PermissionDataRole::getIdType, Collectors.toSet()));
            Map<String, Set<String>> sourceAuthInfo = new HashMap();
            sourceAuthInfo = this.setSourcePermission(rolePermissionDataMap, sourceAuthInfo, (Set)classToPropMap.get(PermissionService.IDTYPE.SOURCE.getName()));
            classAncestorMap = this.setClassPermission(rolePermissionDataMap, allPropMap, allClassMap, (Set)classToPropMap.get(PermissionService.IDTYPE.FIELD.getName()), (Set)classToPropMap.get(PermissionService.IDTYPE.TYPE.getName()));
            var15 = PermissionService.OP.values();
            var16 = var15.length;

            for(var17 = 0; var17 < var16; ++var17) {
                op = var15[var17];
                opType = op.getName();
                opPermission = (Map)result.get(opType);
                opPermission.put("sourceAuthInfo", sourceAuthInfo.get(opType) == null ? new HashSet() : sourceAuthInfo.get(opType));
                opPermission.put("recordAuthInfo", ((Map)recordAuthInfo).get(opType) == null ? new HashSet() : ((Map)recordAuthInfo).get(opType));
                opPermission.put("fieldAuthInfo", classAncestorMap.get(opType) == null ? new HashMap() : classAncestorMap.get(opType));
                result.put(opType, opPermission);
            }
        }

        return result;
    }

    public Map<String, Object> getAllDataPermissionByUserIdAndRoleIdClassV3(List<Integer> roleIds, String userId) throws IOException {
        Map<String, Object> result = new HashMap();
        List<UserSchemaClass> allClass = this.userSchemaClassDao.getAll();
        List<UserSchemaProperty> allProp = this.userSchemaPropertyDao.getAll();
        Map<String, UserSchemaProperty> allPropMap = new HashMap();
        Map<String, UserSchemaClass> allClassMap = new HashMap();
        boolean superAdmin = false;
        Map<String, List<PermissionData>> rolePermissionDataMap = this.getRolePermissionDataMap(roleIds, userId);
        Map<String, Set<String>> recordAuthInfo = new HashMap();
        Map<String, Map<String, Set<String>>> fieldAuthInfo = new HashMap();
        Map<String, Set<String>> sourceAuthInfo = new HashMap();
        if (rolePermissionDataMap.get(PermissionService.IDTYPE.RECORD.getName()) != null) {
            recordAuthInfo = (Map)((List)rolePermissionDataMap.get(PermissionService.IDTYPE.RECORD.getName())).stream().collect(Collectors.groupingBy(PermissionData::getDataId, Collectors.mapping(PermissionData::getOp, Collectors.toSet())));
        }

        Map classToPropMap;
        if (roleIds.contains(1)) {
            superAdmin = true;
            Set<String> allTreeNodes = (Set)this.userTreeNodeDao.getTreeNodes().stream().map((item) -> {
                return item.getId().toString();
            }).collect(Collectors.toSet());
            Iterator var14 = allTreeNodes.iterator();

            while(var14.hasNext()) {
                String node = (String)var14.next();
                ((Map)sourceAuthInfo).put(node, PermissionService.OP.getNames());
            }

            Map<String, Set<String>> allClassPropMap = new HashMap();
            classToPropMap = (Map)allProp.stream().collect(Collectors.groupingBy(UserSchemaProperty::getDomain));
            List<UserSchemaAll> schemaAll = this.userSchemaPermissionService.getSchemaAll();
            Map<String, Set<String>> classAncestorMap = this.userSchemaPermissionService.getAncestorMap(schemaAll);
            allClass.forEach((item) -> {
                if (item.getAtType().equals("Class")) {
                    String className = JacksonUtils.getString(item.getLabel(), "zh-cn");
                    String domain = "[\"" + item.getAtId() + "\"]";
                    List<UserSchemaProperty> propList = new ArrayList();
                    if (classToPropMap.get(domain) != null) {
                        propList.addAll((Collection)classToPropMap.get(domain));
                    }

                    Set<String> classList = (Set)classAncestorMap.get(item.getAtId());
                    if (classList != null) {
                        classList.forEach((classNameData) -> {
                            String parentDomain = "[\"" + classNameData + "\"]";
                            List<UserSchemaProperty> parentPropList = (List)classToPropMap.get(parentDomain);
                            if (parentPropList != null) {
                                propList.addAll(parentPropList);
                            }

                        });
                    }

                    Set<String> propNameList = new HashSet();
                    if (propList != null) {
                        propList.forEach((data) -> {
                            propNameList.add(JacksonUtils.getString(data.getLabel(), "zh-cn"));
                        });
                    }

                    propNameList.addAll(this.getSysProp());
                    allClassPropMap.put(className, propNameList);
                }

            });
            Iterator var18 = allClassPropMap.entrySet().iterator();

            while(var18.hasNext()) {
                Map.Entry<String, Set<String>> entry = (Map.Entry)var18.next();
                Map<String, Set<String>> value = (Map)((Map)fieldAuthInfo).get(entry.getKey());
                if (value == null) {
                    value = new HashMap();
                }

                Iterator var21 = ((Set)entry.getValue()).iterator();

                while(var21.hasNext()) {
                    String prop = (String)var21.next();
                    ((Map)value).put(prop, PermissionService.OP.getNames());
                }

                ((Map)fieldAuthInfo).put(entry.getKey(), value);
            }
        } else {
            allProp.forEach((item) -> {
                allPropMap.put(item.getAtId(), item);
            });
            allClass.forEach((item) -> {
                allClassMap.put(item.getAtId(), item);
            });
            List<String> propType = new ArrayList();
            propType.add(PermissionService.IDTYPE.FIELD.getName());
            propType.add(PermissionService.IDTYPE.SOURCE.getName());
            propType.add(PermissionService.IDTYPE.TYPE.getName());
            Set<PermissionDataRole> openData = this.permissionDataRoleDao.getOpenDataByIdTypes(propType);
            classToPropMap = (Map)openData.stream().collect(Collectors.groupingBy(PermissionDataRole::getIdType, Collectors.toSet()));
            sourceAuthInfo = this.setSourcePermissionV3(rolePermissionDataMap, (Set)classToPropMap.get(PermissionService.IDTYPE.SOURCE.getName()));
            fieldAuthInfo = this.setClassPermissionV3(rolePermissionDataMap, allPropMap, allClassMap, (Set)classToPropMap.get(PermissionService.IDTYPE.FIELD.getName()), (Set)classToPropMap.get(PermissionService.IDTYPE.TYPE.getName()));
        }

        result.put("superAdmin", superAdmin);
        result.put("sourceAuthInfo", sourceAuthInfo);
        result.put("recordAuthInfo", recordAuthInfo);
        result.put("fieldAuthInfo", fieldAuthInfo);
        return result;
    }

    public UserDataPermissionDto getDataPermissionByUserId(String userId) {
        UserDataPermissionDto result = new UserDataPermissionDto();
        this.permissionCacheService.getUserPermission(userId);
        return result;
    }

    @Transactional
    public List<String> pushDataPermissionForNew(List<AuthInfoDto> authInfoDtoList) throws IOException {
        List<String> result = new ArrayList();
        Iterator var3 = authInfoDtoList.iterator();

        while(var3.hasNext()) {
            AuthInfoDto authInfo = (AuthInfoDto)var3.next();
            this.permissionService.deleteDataPermissionByDataId(authInfo);
            this.permissionService.addDataPermissionByDataIdV2(authInfo);
        }

        return result;
    }

    public AuthInfoDto getDataPermissionByDataIdAndIdType(String dataId, String idType) {
        AuthInfoDto authInfo = new AuthInfoDto();
        authInfo.setIsEncryption(1);
        authInfo.setDataId(dataId);
        authInfo.setIdType(idType);
        List<PermissionDataRole> dataList = this.permissionDataRoleDao.getByDataIdAndIdType(dataId, idType);
        List<PermissionDataRole> parentDataList = new ArrayList();
        if (idType.equals(PermissionService.IDTYPE.SOURCE.getName())) {
            Set<String> parentsNodeList = this.userTreeService.getParentsById(Integer.parseInt(dataId));
            if (parentsNodeList != null && parentsNodeList.size() > 0) {
                parentDataList = this.permissionDataRoleDao.getByDataIdsAndIdType((Set)parentsNodeList.stream().map((item) -> {
                    return item.toString();
                }).collect(Collectors.toSet()), idType);
            }

            if (this.customNodeIdEnabled) {
                List<String> nodeIdArr = Arrays.asList(StringUtils.split(this.customNodeIdStr, ","));
                if (nodeIdArr.contains(dataId)) {
                    authInfo.setIsEncryption(0);
                }
            }
        }

        Map<String, List<PermissionDataRole>> dataOpTypeMap = (Map)dataList.stream().collect(Collectors.groupingBy(PermissionDataRole::getOp));
        Map<String, List<PermissionDataRole>> parentDataOpTypeMap = (Map)((List)parentDataList).stream().collect(Collectors.groupingBy(PermissionDataRole::getOp));
        List<AtomDataRolePermissionDto> permissionList = new ArrayList();
        PermissionService.OP[] var9 = PermissionService.OP.values();
        int var10 = var9.length;

        for(int var11 = 0; var11 < var10; ++var11) {
            PermissionService.OP op = var9[var11];
            AtomDataRolePermissionDto permissionItem = new AtomDataRolePermissionDto();
            permissionItem.setOp(op.getName());
            Set<String> inheritRoleIds = new HashSet();
            List itemList;
            Iterator var17;
            PermissionDataRole permissionDataRole;
            if (idType.equals(PermissionService.IDTYPE.SOURCE.getName())) {
                itemList = (List)parentDataOpTypeMap.get(op.getName());
                List<String> parentRoleIdsForOp = new ArrayList();
                if (itemList != null && itemList.size() > 0) {
                    var17 = itemList.iterator();

                    while(var17.hasNext()) {
                        permissionDataRole = (PermissionDataRole)var17.next();
                        if (!permissionDataRole.getRoleId().equals("all")) {
                            parentRoleIdsForOp.add(permissionDataRole.getRoleId());
                        } else {
                            authInfo.setIsEncryption(0);
                        }
                    }
                }

                inheritRoleIds.addAll(parentRoleIdsForOp);
            }

            permissionItem.setInheritRoleIds(inheritRoleIds);
            itemList = (List)dataOpTypeMap.get(op.getName());
            Set<String> roleIdsForOp = new HashSet();
            if (itemList != null && itemList.size() > 0) {
                var17 = itemList.iterator();

                while(var17.hasNext()) {
                    permissionDataRole = (PermissionDataRole)var17.next();
                    if (permissionDataRole.getRoleId().equals("all")) {
                        authInfo.setIsEncryption(0);
                    } else {
                        roleIdsForOp.add(permissionDataRole.getRoleId());
                    }
                }
            }

            roleIdsForOp.addAll(inheritRoleIds);
            permissionItem.setRoleIds(roleIdsForOp);
            permissionList.add(permissionItem);
        }

        authInfo.setPermissions(permissionList);
        List<DataRolePermissionDto> propWrapList = new ArrayList();
        List<PermissionDataRole> oriPropPermissionList = this.permissionDataRoleDao.getByIdType(PermissionService.IDTYPE.FIELD.getName());
        Map<String, List<PermissionDataRole>> propPermissionMap = (Map)oriPropPermissionList.stream().collect(Collectors.groupingBy(PermissionDataRole::getDataId));
        if (idType.equals(PermissionService.IDTYPE.TYPE.getName())) {
            Iterator var34 = propPermissionMap.keySet().iterator();

            label83:
            while(true) {
                String classPropId;
                String classId;
                String propId;
                do {
                    if (!var34.hasNext()) {
                        break label83;
                    }

                    classPropId = (String)var34.next();
                    classId = classPropId.split("@")[0];
                    propId = classPropId.split("@")[1];
                } while(!classId.equals(dataId));

                DataRolePermissionDto propInnerItem = new DataRolePermissionDto();
                propInnerItem.setIsEncryption(1);
                List<AtomDataRolePermissionDto> propPermissionList = new ArrayList();
                List<PermissionDataRole> propOpList = (List)propPermissionMap.get(classPropId);
                Map<String, List<PermissionDataRole>> propOpMap = (Map)propOpList.stream().collect(Collectors.groupingBy(PermissionDataRole::getOp));
                PermissionService.OP[] var20 = PermissionService.OP.values();
                int var21 = var20.length;

                for(int var22 = 0; var22 < var21; ++var22) {
                    PermissionService.OP op = var20[var22];
                    AtomDataRolePermissionDto permissionItem = new AtomDataRolePermissionDto();
                    permissionItem.setOp(op.getName());
                    List<PermissionDataRole> itemList = (List)propOpMap.get(op.getName());
                    Set<String> roleIdsForOp = new HashSet();
                    if (itemList != null && itemList.size() > 0) {
                        Iterator var27 = itemList.iterator();

                        while(var27.hasNext()) {
                            PermissionDataRole permissionDataRole = (PermissionDataRole)var27.next();
                            if (permissionDataRole.getRoleId().equals("all")) {
                                propInnerItem.setIsEncryption(0);
                            } else {
                                roleIdsForOp.add(permissionDataRole.getRoleId());
                            }
                        }
                    }

                    permissionItem.setRoleIds(roleIdsForOp);
                    Set<String> inheritRoleIds = new HashSet();
                    permissionItem.setInheritRoleIds(inheritRoleIds);
                    propPermissionList.add(permissionItem);
                }

                propInnerItem.setPermissions(propPermissionList);
                propInnerItem.setDataId(propId);
                propWrapList.add(propInnerItem);
            }
        }

        authInfo.setPropList(propWrapList);
        return authInfo;
    }
}
